let search_value = '';

// 网关配置的前缀
let account_Server = "/accountSer"
let companyServer = "/companySer"
let jobsServer = "/jobsSer"

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        account_Server = data.accountServer;
        companyServer = data.companyServer;
        jobsServer = data.jobsServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    judgeIsLogin()
    countJob();
    countCompany();
    countHunting();
    getAdvertise();
}

function countJob() {
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/countJob",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("岗位数量");
            if (data.datas === "" ||
                data.datas === null) {
                $("#jobNum").html("0");
            } else {
                $("#jobNum").html(data.datas);
            }
        },
        error: function (err) {
            console.log(err);
            $("#jobNum").html("0");
        }

    });
}

function countHunting() {
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/countHunting",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("工作申请数量");
            if (data.datas === "" ||
                data.datas === null) {
                $("#huntingNum").html("0");
            } else {
                $("#huntingNum").html(data.datas);
            }
        },
        error: function (err) {
            console.log(err);
            $("#huntingNum").html("0");
        }

    });
}

function countCompany() {
    $.ajax({
        type: "get",
        async: false,
        url: companyServer + "/company/countCompany",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("入驻企业");
            if (data.datas === "" ||
                data.datas === null) {
                $("#companyNum").html("0");
            } else {
                $("#companyNum").html(data.datas);
            }
        },
        error: function (err) {
            console.log(err);
            $("#companyNum").html("0");
        }

    });
}

$("#search").click(function () {
    let searchValue = $("#searchValue").val();
    location.href = "/oa/jobs/joblisting/" + searchValue;
    return false;
})


function getAdvertise() {
    $.ajax({
        type: "get",
        async: false,
        url: account_Server + "/account/advertise",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            currentPage: -1,
            level: "二级",
        },
        success: function (data) {
            console.log("二级广告");

            let $advList = $("#advList .swiper");
            ;
            // $advList.empty();
            $.each(data.datas, function (i, item) {
                $advList.append(
                    "<div class='swiper-item'>" +
                    "<img src='" + item.photo + "'>" +
                    "</div>"
                )
            })
            let sw = new Swiper();
            sw.init();
        },
        error: function (err) {
            console.log(err);
        }

    });
}


function judgeIsLogin(){
    let url = window.location.href;
    let urlSplit = url.split("#");
    if (urlSplit[1] !== null){
        if (urlSplit[1] === "l"){
            cocoMessage.info(2000, "登录成功");
        }else if (urlSplit[1] === "o"){
            cocoMessage.info(2000, "登出成功");
        }
    }
}
